﻿using Manox.Libs.ODB;
using System;
using UnityEngine;
using UnityEngine.Networking;


namespace Manox.Libs.Networking
{
    /// <summary>
    /// Unity network writer
    /// </summary>
    public class SerializeWriter : NetworkWriter
    {

        /// <summary>
        /// Write object.
        /// </summary>
        /// <param name="value"></param>
        public void Write(Guid value)
        {
            byte[] data = value.ToByteArray();

            this.WriteBytesFull(data);
        }

        /// <summary>
        /// Write enum.
        /// </summary>
        /// <param name="value"></param>
        public void Write(Enum value)
        {
            this.Write(value.GetType().FullName);
            this.Write(value.ToString());
        }

        /// <summary>
        /// Write object.
        /// </summary>
        /// <param name="value"></param>
        public void WriteObject(object value)
        {
            if (value == null)
                this.Write((Int32)SerializeDataType.Null);
            else if (value is Int32)
            {
                this.Write((Int32)SerializeDataType.Int32);
                this.Write((Int32)value);
            }
            else if (value is Single)
            {
                this.Write((Int32)SerializeDataType.Single);
                this.Write((Single)value);
            }
            else if (value is String)
            {
                this.Write((Int32)SerializeDataType.String);
                this.Write((String)value);
            }
            else if (value is Boolean)
            {
                this.Write((Int32)SerializeDataType.Boolean);
                this.Write((Boolean)value);
            }
            else if (value is Color)
            {
                this.Write((Int32)SerializeDataType.Color);
                this.Write((Color)value);
            }
            else if (value is Vector2)
            {
                this.Write((Int32)SerializeDataType.Vector2);
                this.Write((Vector2)value);
            }
            else if (value is Vector3)
            {
                this.Write((Int32)SerializeDataType.Vector3);
                this.Write((Vector3)value);
            }
            else if (value is Quaternion)
            {
                this.Write((Int32)SerializeDataType.Quaternion);
                this.Write((Quaternion)value);
            }
            else if (value is Guid)
            {
                this.Write((Int32)SerializeDataType.Guid);
                this.Write((Guid)value);
            }
            else if (value is Enum)
            {
                this.Write((Int32)SerializeDataType.Enum);
                this.Write((Enum)value);
            }
            else if (value is ObjectDatabaseItem)
            {
                this.Write((Int32)SerializeDataType.ODBObject);
                this.Write((value as ObjectDatabaseItem).Id);
            }            
        }
    }
}
